﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class LocSiteDA : ILocSiteDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LOC_SITE_ID = "@LOC_SITE_ID";
        private string SQL_CHECK_LOC_SITE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@LOC_SITE WHERE LOC_SITE_ID = @LOC_SITE_ID ";
        private string SQL_DELETE_LOC_SITE = " DELETE FROM @_@LOC_SITE WHERE LOC_SITE_ID = @LOC_SITE_ID ";
        private string SQL_INSERT_LOC_SITE = " INSERT INTO @_@LOC_SITE ( WH_ID, LOC_SITE_ID, DESCR, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @LOC_SITE_ID, @DESCR, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_LOC_SITE = " SELECT  ROWID, WH_ID, LOC_SITE_ID, DESCR, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@LOC_SITE ";
        private string SQL_UPDATE_LOC_SITE = " UPDATE @_@LOC_SITE SET WH_ID = @WH_ID, DESCR = @DESCR, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE LOC_SITE_ID = @LOC_SITE_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckLocSiteIDUnique(string locSiteID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_SITE_ID", locSiteID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_LOC_SITE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckLocSiteIDUnique(DataBase dataBase, DbTransaction tran, string locSiteID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_SITE_ID", locSiteID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_LOC_SITE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteLocSite(List<string> locSiteID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < locSiteID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_SITE_ID", locSiteID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOC_SITE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteLocSite(DataBase dataBase, DbTransaction tran, List<string> locSiteID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < locSiteID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_SITE_ID", locSiteID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOC_SITE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllLocSite(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOC_SITE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllLocSite(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOC_SITE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public LocSiteInfo GetLocSiteByID(string locsiteID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOC_SITE.Replace("@_@", whLoginID) + " WHERE LOC_SITE_ID = @LOC_SITE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_SITE_ID", locsiteID) };
            LocSiteInfo locSiteInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    locSiteInfo = new LocSiteInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOC_SITE_ID"].ToString(), reader["DESCR"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return locSiteInfo;
        }

        public LocSiteInfo GetLocSiteByID(DataBase dataBase, DbTransaction tran, string locsiteID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOC_SITE.Replace("@_@", whLoginID) + " WHERE LOC_SITE_ID = @LOC_SITE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_SITE_ID", locsiteID) };
            LocSiteInfo locSiteInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                locSiteInfo = new LocSiteInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOC_SITE_ID"].ToString(), reader["DESCR"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return locSiteInfo;
        }

        public DataSet GetLocSiteByQueryList(List<string> sqlWhere, LocSiteQueryEntity locsiteQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_LOC_SITE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!locsiteQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, locsiteQuery.CurrentPage, locsiteQuery.PageSize, locsiteQuery.SortField, locsiteQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetLocSiteByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, LocSiteQueryEntity locsiteQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_LOC_SITE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, locsiteQuery.CurrentPage, locsiteQuery.PageSize, locsiteQuery.SortField, locsiteQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertLocSite(LocSiteInfo locSiteInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locSiteInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LocSite_Parameters(locSiteInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOC_SITE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertLocSite(DataBase dataBase, DbTransaction tran, LocSiteInfo locSiteInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locSiteInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LocSite_Parameters(locSiteInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOC_SITE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_LocSite_Parameters(LocSiteInfo locSiteInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.LOC_SITE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.DESCR, SqlDbType.VarChar, 100), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(locSiteInfo.WhID))
            {
                paramArray[0].Value = locSiteInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = locSiteInfo.LocSiteID;
            if (!string.IsNullOrEmpty(locSiteInfo.Descr))
            {
                paramArray[2].Value = locSiteInfo.Descr;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = locSiteInfo.IsActive;
            paramArray[4].Value = locSiteInfo.CreatedBy;
            paramArray[5].Value = locSiteInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateLocSite(LocSiteInfo locSiteInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locSiteInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LocSite_Parameters(locSiteInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOC_SITE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateLocSite(DataBase dataBase, DbTransaction tran, LocSiteInfo locSiteInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locSiteInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LocSite_Parameters(locSiteInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOC_SITE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

